package com.xxh.food.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xxh.food.common.PagePARAM;
import com.xxh.food.common.Result;
import com.xxh.food.common.entity.Food;
import com.xxh.food.common.param.FoodPARAM;
import com.xxh.food.service.FoodService;

import io.swagger.annotations.Api;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


@RestController
@RequestMapping(value = "/food")
@Api(tags = "美食模块接口")
public class FoodController {

    @Resource
    private FoodService foodService;


    @ApiOperation(value = "查询美食总数")
    @GetMapping(value = "/num")
    public Result<?> getFoodNum(){
        int num = foodService.foodNum();
        if (num > 0) {
            return Result.success(num);
        } else {
            return Result.error("-1", "查询失败");
        }
    }


    @ApiOperation(value = "添加美食")
    @PostMapping(value = "/add")
    public Result<?> addFood(@RequestBody FoodPARAM foodPARAM){
        Food food =new Food();
        BeanUtils.copyProperties(foodPARAM,food);
        foodService.save(food);
        return Result.success(food);
    }

    @ApiOperation(value = "更新美食")
    @PutMapping(value = "/update")
    public Result<?> updateFood(@RequestBody FoodPARAM foodPARAM){
        Food food =new Food();
        BeanUtils.copyProperties(foodPARAM,food);
        foodService.saveOrUpdate(food);
        return Result.success(food);
    }

    @ApiOperation(value = "根据Id查询美食")
    @GetMapping(value = "/{id}")
    public Result<?> findUserById(@PathVariable String id){
        Food food = foodService.getById(id);
        return Result.success(food);
    }

    @ApiOperation(value = "根据Id删除美食")
    @ApiImplicitParam(name = "ids",value = "美食id集合",required = true)
    @DeleteMapping(value = "/delete/{id}")
    public Result<?> deleteUserById(@PathVariable String id){
        foodService.removeById(id);
        return Result.success();
    }

    @ApiOperation(value = "分页查询")
    @PostMapping("/page")
    public Result<?> findUserByPage(@RequestBody PagePARAM<FoodPARAM> pagePARAM){
        Page<Food> page = foodService.findFoodByPage(pagePARAM.getPageNum(),pagePARAM.getPageSize(),pagePARAM.getSearchPARAM());
        return Result.success(page);
    }

}
